﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace ex_uppgift
{
    public class ContactManagement
    {
        public void CreateContact()
        {
            try
            {
                Contact contact = new Contact();

                contact.Name = CRM_System.form.tbxAddName.Text;
                contact.EmailAddress = CRM_System.form.tbxAddEmailaddress.Text;
                contact.CompanyName = CRM_System.form.tbxAddCompany.Text;
                contact.PersonId = long.Parse(CRM_System.form.tbxAddId.Text);
                contact.PhoneNumber = long.Parse(CRM_System.form.tbxAddPhoneNumber.Text);
                contact.MonthBirthday = int.Parse(CRM_System.form.cbxAddBirthdayMonth.SelectedItem.ToString());
                contact.DayBirthday = int.Parse(CRM_System.form.cbxAddBirthdayDay.SelectedItem.ToString());
                contact.Hobbies = CRM_System.form.tbxAddHobbies.Text;
                contact.City = CRM_System.form.tbxAddCity.Text;
                contact.Street = CRM_System.form.tbxAddStreet.Text;
                contact.ZipCode = int.Parse(CRM_System.form.tbxAddZipcode.Text);

                Form1.contacts.Add(contact);

                Serializer.SerializeContacts();

                MessageBox.Show("Contact with Name: " + CRM_System.form.tbxAddName.Text + " and ID: " + CRM_System.form.tbxAddId.Text + " was added to your contact list.");

                CRM_System.form.EmptyAddContactFields();

                Serializer.DeserializeContacts();
            }
            catch
            {
                MessageBox.Show("Invalid input");
            }
        }

        public void BirthdayReminder()
        {
        }

        public void RemoveContact()
        {
            if (MessageBox.Show("Really delete?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Form1.contacts.RemoveAt(CRM_System.form.lbxContactList.SelectedIndex);
                Serializer.SerializeContacts();
                CRM_System.form.RemoveButtonVisibiltity();
            }
        }

        public void AddRelative()
        {
            if (CRM_System.form.tbxRelativeName.Text == String.Empty ||
                CRM_System.form.tbxRelativeId.Text == String.Empty ||
                    CRM_System.form.cbxAddRelativeBirthDay.SelectedItem == null ||
                    CRM_System.form.cbxAddRelativeBirthMonth.SelectedItem == null ||
                    CRM_System.form.cbxTypeOfRelative.SelectedItem == null)
            {
                MessageBox.Show("You have to fill all fields");
            }

            else
            {
                var addContactRelative = Form1.contacts
            .Where(contact => contact.PersonId == (CRM_System.form.lbxContactList.SelectedItem as Contact).PersonId)
            .Select(contact => contact.PersonId).FirstOrDefault();

                Relative relative = new Relative();

                relative.Name = CRM_System.form.tbxRelativeName.Text;
                relative.PersonId = long.Parse(CRM_System.form.tbxRelativeId.Text);
                relative.MonthBirthday = int.Parse(CRM_System.form.cbxAddRelativeBirthMonth.SelectedItem.ToString());
                relative.DayBirthday = int.Parse(CRM_System.form.cbxAddRelativeBirthDay.SelectedItem.ToString());
                relative.WhosRelative = addContactRelative;
                relative.TypeOfFamily = CRM_System.form.cbxTypeOfRelative.SelectedItem.ToString();

                Form1.relatives.Add(relative);

                Serializer.SerializeRelative();
                //bindas till lista.
            }
        }

        public void RemoveRelative()
        {
            if (MessageBox.Show("Would you like to delete?", "Confirm Delete Contact Action", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Form1.relatives.RemoveAt(CRM_System.form.lbxRelatives.SelectedIndex);
                Serializer.DeserializeRelatives();
                CRM_System.form.lbxRelatives.Update();
            }
        }


    }
}